package com.zhiqianbao.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.zhiqianbao.R;
import com.zhiqianbao.entity.Repay;
import com.zhiqianbao.entity.Result;
import com.zhiqianbao.entity.UserBank;
import com.zhiqianbao.utils.Constants;
import com.zhiqianbao.utils.HttpUtils;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import cn.trinea.android.common.util.StringUtils;

/**
 * Created by liang on 2016/1/15.
 */
public class RepayBankConfirmActivity extends BaseActivity implements View.OnClickListener {
    //本期信息
    Repay repay;
    //银行卡信息
    UserBank userBank;
    /**
     * 显示金额
     */
    TextView textViewLineOfPayMoneyContent;
    /**
     * 显示银行
     */
    TextView textViewPayBankContent;
    /**
     * 显示银行卡号
     */
    TextView textView_payBankNumberContent;
    /**
     * 确认支付按钮
     */
    Button buttonConfirm;
    /**
     * 取消按钮
     */
    Button buttonCancel;
    String bankcard;

    //支付处理handler
    Handler payHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            closeWaitDialog();
            switch (msg.what) {
                case RESULT_CANCELED :
                    showShortToast1(Constants.HTTP_ERROR_MESSAGE);
                    break;
                case RESULT_OK :
                    Result<HashMap> data = (Result<HashMap>) msg.obj;
                    if(data!=null) {
                        if (data.getCode().equals(Constants.DEFAULT_SUCCESS_CODE)) {
                            //请求成功 跳转到 输入验证码页面
                            String dataStr = gson.toJson(data.getData());
                            Intent confirmPay = new Intent(RepayBankConfirmActivity.this, PayPhoneConfirmActivity.class);
                            confirmPay.putExtra("dataStr", dataStr);
                            startActivity(confirmPay);
                            RepayBankConfirmActivity.this.finish();
                        } else {
                            showShortToast1(data.getMsg());
                        }
                    }
                    break;
            }

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repay_confirm_bank);
        init();
        Intent intent = getIntent();
        repay = (Repay) intent.getSerializableExtra("repay");
        userBank = (UserBank) intent.getSerializableExtra("userBank");
        initView();
    }

    private void initView() {
         textViewLineOfPayMoneyContent=(TextView)findViewById(R.id.textView_line_of_pay_money_content);
         textViewPayBankContent=(TextView)findViewById(R.id.textView_pay_bank_content);
         textView_payBankNumberContent=(TextView)findViewById(R.id.textView_pay_bank_number_content);
         buttonConfirm=(Button)findViewById(R.id.button_confirm);
         buttonCancel=(Button)findViewById(R.id.button_cancel);

        //金额
        DecimalFormat df = new DecimalFormat("#.00");
        String moneyStr = df.format(repay.getRepayTotal());
        textViewLineOfPayMoneyContent.setText(moneyStr);
        textViewPayBankContent.setText(userBank.getDeposit());
        bankcard = userBank.getBankCard();
        int l = bankcard.length() -  8;
        String x = "";
        for (int i = 0; i < l; i++) {
            x += "*";
        }
        bankcard = bankcard.substring(0, 4) + x + bankcard.substring(bankcard.length() - 4, bankcard.length());
        textView_payBankNumberContent.setText(bankcard);
        buttonCancel.setOnClickListener(this);
        buttonConfirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(R.id.button_confirm==v.getId()) {
            //调用获取支付验证码接口
            showWaitDialog();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Message message = new Message();
                    Map<String, String> params = new HashMap<>();
                    //Long repayId, String bankCard, String amount
                    params.put("token", Constants.LOGIN_USER.getToken());
                    params.put("userId", Constants.LOGIN_USER.getId() + "");
                    params.put("repayId", repay.getId() + "");
                    params.put("bankCard", userBank.getBankCard());
                    params.put("amount", "1");
                    String result = HttpUtils.httpPostString(Constants.API_ROOT + "/payment/getDynNum", params);
                    if (StringUtils.isBlank(result)) {
                        message.what = RESULT_CANCELED;
                    } else {
                        message.what = RESULT_OK;
                        Result<HashMap> data = Result.fromJson(result, HashMap.class);
                        message.obj = data;
                    }
                    payHandler.sendMessage(message);
                }
            }).start();
        }
        else if(R.id.button_cancel==v.getId()){
                finish();
        }
    }
}
